/**
 * Created by lenovo on 2017/5/8.
 * 顶层门店（storePage）组件，包含多个子组件
 * 组件1：storePageHeader.react.js（page页面上方的header组件）
 * 组件2：StoreSwitchTab.react.js（门店全部商品和全部点评切换模块）
 */

import React from 'react';
import StorePageHeader from './storePageHeader.react';
//import BottomBar from './bottomBar.react';
import StoreSwitchTab from './storeSwitchTab.react';

//import DownloadApp from '../downloadApp.react';

class AppStorePage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            headerData: {},
            evaluateData: {},
            commodityData: {}
        };
        //私有方法需要在constructor当中bind
        this.getPostData = this.getPostData.bind(this);
        this.getGetData = this.getGetData.bind(this);
        this.queryStoreData = this.queryStoreData.bind(this);
        this.queryEvaluateData = this.queryEvaluateData.bind(this);
        this.queryCommodityData = this.queryCommodityData.bind(this);
    }


    //fetch函数统一封装
    getPostData(url, ...argument) {
        let fetchQuery = fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
            },
            body: JSON.stringify({
                argument
            })
        });
        return fetchQuery;
    }

    getGetData(url, ...argument) {
        let fetchQuery;
        try {
            fetchQuery = fetch(
                url + argument,
                {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                        'Accept': 'application/json',
                    }
                });
            return fetchQuery;
        } catch (e) {
            console.log('GETQuery error' + e);
        }
    }

    //请求门店数据并分发
    //queryStoreData(uuid)
    queryStoreData(url, ...queryData) {
        let query = this.getGetData(url, queryData);
        if (query instanceof Promise) {
            return query
                .then((response) => response.json())
                .then((responseJson) => {
                    if (responseJson.data != null) {

                        this.setState({
                            headerData: responseJson.data.shop_info
                        });
                    }
                    else{
                        this.setState({
                            headerData: {}
                        });
                    }
                })
                .catch((e) => {
                    console.log('mx_error: ' + e);
                });
        } else {
            console.log('stop error');
        }
    }

    //请求评论数据并分发
    queryEvaluateData(url, ...queryData) {
        let query = this.getGetData(url, queryData);
        if (query instanceof Promise) {
            return query
                .then((response) => response.json())
                .then((responseJson) => {
                    if (responseJson !== undefined) {
                        this.setState({
                            evaluateData: responseJson.data.comment_list
                        });
                    }
                }).catch((e) => {
                    console.log('error' + e);
                });
        } else {
            console.log('stop error');
        }
    }

    //请求商品数据并分发
    queryCommodityData(url, ...queryData) {
        let query = this.getGetData(url, queryData);
        if (query instanceof Promise) {
            return query
                .then((response) => response.json())
                .then((responseJson) => {
                    if (responseJson !== undefined) {
                        this.setState({
                            commodityData: responseJson
                        });
                    }
                })
                .catch((e) => {
                    console.log('error' + e);
                });
        } else {
            console.log('stop error');
        }
    }

    //初始化数据
    componentDidMount() {
        //render渲染之前被调用，可以在渲染之前做准备工作
        // getData(url, queryData)
        //请求门店数据
        //POST请求

        /*this.queryStoreData(
            env_host + '/api/e-commerce/shop/get-info?shop_uuid=' + shop_uuid
        ).then(
            //请求评论数据
            //GET请求使用uuid来确定具体的店铺
            this.queryEvaluateData(
                env_host + '/api/e-commerce/comment/get-list?uuid=' + shop_uuid
            )
        ).then(
            //请求商品数据
            //GET请求使用product_list_id确定门店对应的商品
            //('http://{{hostname}}/api/e-commerce/product/list/get-info?product_list_id='+this.state.headerData.brandId+'&XDEBUG_SESSION_START')
            this.queryCommodityData(
                env_host + '/api/e-commerce/product/list/get-info?product_list_id=' + brand_id
            )
        ).catch((e) => {
            console.log("queryData error" + e);
        });*/
    }

    render() {

        return (
            <div className="wrapContainer">

                <div className="container">

                    <div className="storePageTop">
                        <StorePageHeader headerData={this.state.headerData}/>
                        {/*<StorePageHeader />*/}
                    </div>
                    <div className="cutRules"></div>
                    <div className="StorePageEvaluateContainer">
                        <StoreSwitchTab
                            evaluateData={this.state.evaluateData}
                            commodityData={this.state.commodityData}
                        />
                    </div>
                </div>
                {/*<BottomBar />*/}

                {/*<DownloadApp eventInfo={'长泰广场街拍活动'}/>*/}
            </div>
        )
    }
}

export default AppStorePage;